//
//  GJFVideoCollectionViewCell.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/29.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFVideoCollectionViewCell.h"
@class GJFVideoViewController;

@interface GJFVideoCollectionViewCell ()
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UILabel *duration;
@property (nonatomic, strong) UITextView *sourcename;
@property (nonatomic, strong) UIImageView *talk;
@property (nonatomic, strong) UILabel *talkcount;
@property (nonatomic, strong) UIButton *like;
@property (nonatomic, strong) UILabel *likecount;
@property (nonatomic, strong) UIButton *share;
@end

@implementation GJFVideoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)setVideoModel:(GJFVideoModel *)videoModel {
    _videoModel = videoModel;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:videoModel.imglink] placeholderImage:[UIImage imageNamed:@"占位图"]];
    self.duration.text = videoModel.duration;
    self.sourcename.text = videoModel.title;
    self.talkcount.text = videoModel.readarts;
    self.likecount.text = videoModel.likecount;
    self.like.selected = [videoModel.is_collect boolValue];
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.imageV.frame = CGRectMake(0, 0, self.contentView.frame.size.width, (self.contentView.frame.size.width) / 5 * 3);
    self.duration.frame = CGRectMake(self.imageV.frame.size.width - 50 * XX, self.imageV.frame.size.height - 20 * YY, 50 * XX, 20 * YY);
    self.sourcename.frame = CGRectMake(0, self.imageV.frame.size.height, self.imageV.frame.size.width, 45 * YY);
    self.talk.frame = CGRectMake(10 * XX, self.sourcename.frame.size.height +self.sourcename.frame.origin.y, 20 * XX, 20 * XX);
    self.talkcount.frame = CGRectMake(35 * XX, self.sourcename.frame.size.height +self.sourcename.frame.origin.y, 35 * XX, 20 * XX);
    self.like.frame = CGRectMake(110 * XX, self.sourcename.frame.size.height +self.sourcename.frame.origin.y, 20 * XX, 20 * XX);
    self.likecount.frame = CGRectMake(133 * XX, self.sourcename.frame.size.height +self.sourcename.frame.origin.y, 20 * XX, 20 * XX);
    self.share.frame = CGRectMake(150 * XX, self.sourcename.frame.size.height +self.sourcename.frame.origin.y + 3 * XX, 14 * XX, 14 * XX);
}

#pragma mark - 点击分享
- (void)shareAction {
    [GJFSingleTool shareToViewController:(GJFVideoViewController *)[GJFSingleTool topViewController] title:@"幻紫琉晶" content:self.videoModel.sourcename image:nil url:self.videoModel.url];
}

- (void)collectAction {
    NSDictionary *dict = @{@"collectId" : self.videoModel.id?:@"", @"type":@"2"};
    NSString *url = self.like.isSelected?APP_PLOT_UNCOLLECT:APP_PLOT_COLLECT;
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([result[@"code"] intValue] == 1000) {
            self.like.selected = !self.like.selected;
            self.videoModel.is_collect = [NSString stringWithFormat:@"%d", ![self.videoModel.is_collect boolValue]];
        }
        [[[GJFSingleTool topViewController] view] showWarning:result[@"msg"]];
    }];
}

- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageV];
    }
    return _imageV;
}
- (UILabel *)duration {
    if (!_duration) {
        _duration = [[UILabel alloc] init];
        _duration.backgroundColor = [UIColor blackColor];
        _duration.textAlignment = NSTextAlignmentRight;
        _duration.font = [UIFont systemFontOfSize:15 * XX];
        _duration.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_duration];
    }
    return _duration;
}
- (UITextView *)sourcename {
    if (!_sourcename) {
        _sourcename = [[UITextView alloc] init];
        _sourcename.userInteractionEnabled = NO;
        _sourcename.textAlignment = NSTextAlignmentCenter;
        _sourcename.contentInset = UIEdgeInsetsMake(-5 * XX, 0, 0, 0);
        _sourcename.font = [UIFont systemFontOfSize:15 * XX];
        [self.contentView addSubview:_sourcename];
    }
    return _sourcename;
}
- (UIImageView *)talk {
    if (!_talk) {
        _talk = [[UIImageView alloc] init];
        _talk.image = [UIImage imageNamed:@"播放"];
        [self.contentView addSubview:_talk];
    }
    return _talk;
}
- (UILabel *)talkcount {
    if (!_talkcount) {
        _talkcount = [[UILabel alloc]init];
        _talkcount.font = [UIFont systemFontOfSize:15 * XX];
        [self.contentView addSubview:_talkcount];
    }
    return _talkcount;
}
- (UIButton *)like {
    if (!_like) {
        _like = [[UIButton alloc] init];
        [_like setBackgroundImage:[UIImage imageNamed:@"icon-收藏"] forState:UIControlStateNormal];
        [_like setBackgroundImage:[UIImage imageNamed:@"icon-已收藏"] forState:UIControlStateSelected];
        [_like addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_like];
    }
    return _like;
}
- (UILabel *)likecount {
    if (!_likecount) {
        _likecount = [[UILabel alloc] init];
        _likecount.font = [UIFont systemFontOfSize:15 * XX];
        [self.contentView addSubview:_likecount];
    }
    return _likecount;
}
- (UIButton *)share {
    if (!_share) {
        _share = [[UIButton alloc] init];
        [_share setBackgroundImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
        [_share addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_share];
    }
    return _share;
}
@end
